package com.icez.ball_ripple_progress_library

import android.view.View

class Utils {
    companion object {
        val instance: Utils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
             Utils()
        }
    }

    /**
     * 用于自定义控件，计算控件的宽度，在onMeasure中setMeasuredDimension(measureViewWidth,measureViewHeight)
     *
     * @param widthMeasureSpec 宽度的模式
     * @param onGetWidthForNotExactly 除MeasureSpec.EXACTLY模式的其他模式设置指定控件的大小
     * @return
     */
    public fun measureViewWidth( widthMeasureSpec:Int, onGetWidthForNotExactly:((specSize:Int)->Int)?): Int {
        val specModel = View.MeasureSpec.getMode(widthMeasureSpec)
        val specSize = View.MeasureSpec.getSize(widthMeasureSpec)
        when(specModel){
            View.MeasureSpec.EXACTLY ->{
                return specSize
            }
            View.MeasureSpec.AT_MOST ->{
                return if (onGetWidthForNotExactly != null) {
                    onGetWidthForNotExactly(specSize)
                }else{
                    specSize
                }
            }
            else ->{ // MeasureSpec.UNSPECIFIED
                return if (onGetWidthForNotExactly != null) {
                    onGetWidthForNotExactly(specSize)
                }else{
                    specSize
                }
            }
        }


    }

    /**
     * 用于自定义控件，计算控件的高度，在onMeasure中setMeasuredDimension(measureViewWidth,measureViewHeight)
     *
     * @param heightMeasureSpec 高度度的模式
     * @param onGetHeightForNotExactly 除MeasureSpec.EXACTLY模式的其他模式设置指定控件的大小
     * @return
     */
    public fun measureViewHeight(heightMeasureSpec:Int,onGetHeightForNotExactly:((specSize:Int)->Int)?): Int {
        val specModel = View.MeasureSpec.getMode(heightMeasureSpec)
        val specSize = View.MeasureSpec.getSize(heightMeasureSpec)
        when(specModel){
            View.MeasureSpec.EXACTLY ->{
                return specSize
            }
            View.MeasureSpec.AT_MOST ->{
                return if (onGetHeightForNotExactly != null) {
                    onGetHeightForNotExactly(specSize)
                }else{
                    specSize
                }
            }
            else ->{ // MeasureSpec.UNSPECIFIED
                return if (onGetHeightForNotExactly != null) {
                    onGetHeightForNotExactly(specSize)
                }else{
                    specSize
                }
            }
        }
    }
}